package com.example.audiostreaming.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.audiostreaming.R;
import com.example.audiostreaming.views.ViewPopWindow;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

import static com.example.audiostreaming.base.BaseApplication.getAppContext;

public class AdapterPlayerPlayListRv extends RecyclerView.Adapter<AdapterPlayerPlayListRv.InnerHolder> {
    private List<Track> mPlayListData= new ArrayList<>();
    private int playingIndex = 0;
    private ViewPopWindow.OnPlayListItemClickListener mPlayListItemClickListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_player_pop_play_list_item, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        holder.setUIData(mPlayListData.get(position),position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPlayListItemClickListener != null) {
                    mPlayListItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mPlayListData.size();
    }

    public void setData(List<Track> playListData) {
        //设置播放列表数据，更新列表
        mPlayListData.clear();
        mPlayListData.addAll(playListData);
        notifyDataSetChanged();
    }

    public void setCurrentPosition(int position) {
        playingIndex = position;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(ViewPopWindow.OnPlayListItemClickListener playListItemClickListener) {
        mPlayListItemClickListener = playListItemClickListener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setUIData(Track track, int position) {
            TextView playListItemTitle = itemView.findViewById(R.id.play_list_item_title_tv);
            playListItemTitle.setText(track.getTrackTitle());
            playListItemTitle.setTextColor(getAppContext().getResources()
                    .getColor(playingIndex == position ? R.color.color_Main : R.color.color_Gray));
            ImageView playListItemIcon = itemView.findViewById(R.id.play_list_item_icon_iv);
            playListItemIcon.setVisibility(playingIndex == position ? View.VISIBLE : View.GONE);
        }
    }
}
